package com.kakavr.staff.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.staff.service.IStaffAdminService;
import com.kakavr.util.ValidatorUtils;

/**
 * 员工、合伙人管理
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("staffAdmin")
public class StaffAdminController extends BaseController {
	@Resource
	private IStaffAdminService staffAdminService;

	/**
	 * 添加员工
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addStaff", method = RequestMethod.POST)
	public Map<String, Object> addStaff(HttpServletRequest request) {
		String roleid = request.getParameter("roleId");
		if (StringUtils.isBlank(roleid)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "角色");
		}
		Map<String, Object> map = this.getAddAdminParam(request);
		map.put("ispartner", 0);
		map.put("roleid", roleid);
		staffAdminService.addStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 添加合伙人
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addPartner", method = RequestMethod.POST)
	public Map<String, Object> addPartner(HttpServletRequest request) {
		Map<String, Object> map = this.getAddAdminParam(request);
		map.put("ispartner", 1);
		staffAdminService.addStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改员工
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateStaff", method = RequestMethod.POST)
	public Map<String, Object> updateStaff(HttpServletRequest request) {
		String roleid = request.getParameter("roleId");
		if (!ValidatorUtils.isNumber(roleid)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "角色");
		}
		Map<String, Object> map = this.getUpdateAdminParam(request);
		map.put("roleid", roleid);
		staffAdminService.updateStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改合伙人
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePartner", method = RequestMethod.POST)
	public Map<String, Object> updatePartner(HttpServletRequest request) {
		Map<String, Object> map = this.getUpdateAdminParam(request);
		staffAdminService.updateStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 添加员工参数处理
	 * @param request
	 * @return
	 */
	private Map<String, Object> getAddAdminParam(HttpServletRequest request) {
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "姓名");
		}
		String username = request.getParameter("userName");
		if (StringUtils.isBlank(username)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "用户名");
		}
		String mobile = request.getParameter("mobile");
		if (StringUtils.isBlank(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "手机号");
		}
		if (!ValidatorUtils.isMobile(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "手机号");
		}
		String status = request.getParameter("status");
		if (!ValidatorUtils.isNumber(status)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "账号状态");
		}
		// 是否驻店0：否 1：是
		String isResident = request.getParameter("isResident");
		if (StringUtils.isBlank(isResident)) {
			isResident = "0";
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", username);
		map.put("name", name);
		map.put("mobile", mobile);
		map.put("status", status);
		map.put("isResident", isResident);
		map.put("password", EncryptionUtils.DEFAULT_PASS);
		map.put("gender", request.getParameter("gender"));
		return map;
	}

	/**
	 * 修改员工参数处理
	 * @param request
	 * @return
	 */
	private Map<String, Object> getUpdateAdminParam(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "姓名");
		}
		String mobile = request.getParameter("mobile");
		if (StringUtils.isBlank(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "手机号");
		}
		if (!ValidatorUtils.isMobile(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "手机号");
		}
		String status = request.getParameter("status");
		if (!ValidatorUtils.isNumber(status)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "账号状态");
		}
		// 是否驻店0：否 1：是
		String isResident = request.getParameter("isResident");
		if (StringUtils.isBlank(isResident)) {
			isResident = "0";
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("name", name);
		map.put("mobile", mobile);
		map.put("status", status);
		map.put("isResident", isResident);
		map.put("gender", request.getParameter("gender"));
		return map;
	}

	/**
	 * 员工、合伙人分页列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectStaff", method = RequestMethod.GET)
	public Map<String, Object> selectStaff(HttpServletRequest request) {
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("ispartner", request.getParameter("ispartner"));
		return MsgResult.okResultData(staffAdminService.selectStaffByPage(map));
	}

	/**
	 * 修改员工帐号状态
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	public Map<String, Object> updateStaffStatus(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		String status = request.getParameter("status");
		if (!ValidatorUtils.isNumber(status)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "账号状态");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("status", status);
		staffAdminService.updateStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 重置密码
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "resetPass", method = RequestMethod.POST)
	public Map<String, Object> resetPass(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("password", EncryptionUtils.DEFAULT_PASS);
		staffAdminService.updateStaff(map);
		return MsgResult.okResult();
	}

	/**
	 * 合伙人列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectPartner", method = RequestMethod.GET)
	public Map<String, Object> selectPartner(HttpServletRequest request) {
		return MsgResult.okResultData(staffAdminService.selectPartnerList());
	}

	/**
	 * 删除员工
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteStaff", method = RequestMethod.POST)
	public Map<String, Object> deleteStaff(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		staffAdminService.deleteStaff(map);
		return MsgResult.okResult();
	}
}
